﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Simp.CodeModel.CoreFunctions.Sequences
{
	/// <summary>Encapsulates a function selecting a sub range of a sequence.</summary>
	public class RangeSelectorFunction : IFunction, ISequenceFunction, IFunctionSerializer
	{
		#region Inner Types
		private class RangeSelectorSequenceAccessor : ISequenceAccessor
		{
			private readonly ISequenceAccessor _sourceAccessor;
			private readonly int _first;
			private readonly int _last;

			public RangeSelectorSequenceAccessor(ISequenceAccessor sourceAccessor, int first, int last)
			{
				_sourceAccessor = sourceAccessor;
				_first = first;
				_last = last;
			}

			#region ISequenceAccessor Members
			int ISequenceAccessor.Count
			{
				get { return Math.Abs(_last - _first) + 1; }
			}

			IFunction ISequenceAccessor.this[int index]
			{
				get
				{
					if (_last >= _first)
					{
						return _sourceAccessor[index + _first];
					}
					else
					{
						return _sourceAccessor[_first - index];
					}
				}
			}
			#endregion

			#region IEnumerable<IFunction> Members
			IEnumerator<IFunction> IEnumerable<IFunction>.GetEnumerator()
			{
				if (_last >= _first)
				{
					int index = 0;

					foreach (IFunction sourceElement in _sourceAccessor)
					{	//	Skip the ones before the first
						if (index >= _first)
						{
							yield return sourceElement;
						}
						++index;
						if (index > _last)
						{
							break;
						}
					}
				}
				else
				{	//	We have to buffer the sub-source range in order to reverse it
					IFunction[] bufferedFunctionList = new IFunction[((ISequenceAccessor)this).Count];
					int index = 0;

					foreach (IFunction sourceElement in _sourceAccessor)
					{	//	Skip the ones before the first
						if (index >= _last)
						{
							bufferedFunctionList[_first - index] = sourceElement;
						}
						++index;
						if (index > _first)
						{
							break;
						}
					}

					foreach (IFunction sourceElement in bufferedFunctionList)
					{
						yield return sourceElement;
					}
				}
			}
			#endregion

			#region IEnumerable Members
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<IFunction>)this).GetEnumerator();
			}
			#endregion
		}
		#endregion

		private static readonly string[] PARAMETER_NAMES = new[] { "source", "first", "last" };

		/// <summary>Name of the function.</summary>
		public const string RangeSelectorFunctionName = "SequenceRange";

		#region IFunction Members
		string IFunction.FunctionName
		{
			get { return RangeSelectorFunctionName; }
		}

		string[] IFunction.ParameterNames
		{
			get { return PARAMETER_NAMES; }
		}

		IFunctionParameterBinding[] IFunction.DefaultParameterValues
		{
			get { return FunctionHelper.EmptyBindings; }
		}

		IFunction IFunction.Simplify(SimplificationMode simplificationMode, params IFunction[] parameters)
		{
			return null;
		}

		IFunction IFunction.AcceptVisitor(IFunctionVisitor visitor)
		{
			return null;
		}
		#endregion

		#region ISequenceFunction Members
		ISequenceAccessor ISequenceFunction.GetAccessor(params IFunction[] parameters)
		{
			FunctionHelper.ValidateParameters(this, parameters);

			ISequenceAccessor accessor = FunctionHelper.GetAccessor(parameters[0]);
			IntegerFunction first = parameters[1] as IntegerFunction;
			IntegerFunction last = parameters[2] as IntegerFunction;

			if (accessor == null || first == null || last == null)
			{
				return null;
			}
			else
			{
				int count = accessor.Count;
				int firstValue = first.Value;
				int lastValue = last.Value;

				if (firstValue >= count || firstValue < -count || lastValue >= count || lastValue < -count)
				{
					return null;
				}
				else
				{
					return new RangeSelectorSequenceAccessor(
						accessor,
						firstValue >= 0 ? firstValue : count + firstValue,
						lastValue >= 0 ? lastValue : count + lastValue);
				}
			}
		}
		#endregion

		#region IFunctionSerializer Members
		void IFunctionSerializer.Serialize(AppendOnlyStringBuilder builder, params IFunction[] parameters)
		{
			FunctionHelper.ValidateParameters(this, parameters);

			IFunction sequence = parameters[0];
			IFunction first = parameters[1];
			IFunction last = parameters[2];

			FunctionHelper.Serialize(sequence, builder);
			builder.Append("[");
			FunctionHelper.Serialize(first, builder);
			builder.Append(":");
			FunctionHelper.Serialize(last, builder);
			builder.Append("]");
		}
		#endregion
	}
}